package com.behome.netty.netty_B.bean;

import com.behome.netty.common.ByteConventerKit;
import lombok.extern.slf4j.Slf4j;

import java.nio.ByteBuffer;

@Slf4j
public class ReBackPacketHead {
    public static final byte[] FLAG = new byte[]{-2, -3};
    public static final short LENGTH = 11;
    static final byte[] VERSION = new byte[]{1, 0};
    public static final int INDEX_BODY_LENGTH = 9;
    private int sessionId;
    private byte type;
    private short packetLength;

    public ReBackPacketHead() {
    }

    public ReBackPacketHead(int sessionId, PacketType packetType, short packetLength) {
        this.sessionId = sessionId;
        this.type = packetType.value;
        this.packetLength = packetLength;
    }

    public int getSessionId() {
        return this.sessionId;
    }

    public void setSessionId(int sessionId) {
        this.sessionId = sessionId;
    }

    public PacketType getType() {
        return PacketType.getPacketTypeByValue(this.type);
    }

    public void setType(PacketType type) {
        this.type = type.value;
    }

    public int getPakcetLength() {
        return this.packetLength;
    }

    public void setPacketLength(short packetLength) {
        this.packetLength = packetLength;
    }

    public byte[] serialize() {
        PacketType tmpPacketType = PacketType.getPacketTypeByValue(this.type);
        if(tmpPacketType == PacketType.UNKNOWN) {
            throw new IllegalArgumentException("数据包类型type为非法值,不能序列化消息头.");
        } else if(this.packetLength <= 0) {
            throw new IllegalArgumentException("数据包长度packetLength不能为空,不能序列化消息头.");
        } else {
            ByteBuffer byteBuffer = ByteBuffer.allocate(11);
            byteBuffer.put(FLAG).put(VERSION).put(ByteConventerKit.intToBytes2(this.sessionId)).put(this.type).put(ByteConventerKit.shortToBytes(this.packetLength));
            return byteBuffer.array();
        }
    }

    public static ReBackPacketHead deserialize(byte[] headData) {
        ReBackPacketHead ret = new ReBackPacketHead();
        if(headData != null && headData.length == 11) {
            ByteBuffer byteBuffer = ByteBuffer.wrap(headData);
            ret.setSessionId(byteBuffer.getInt(4));
            ret.setType(PacketType.getPacketTypeByValue(byteBuffer.get(8)));
            ret.setPacketLength(byteBuffer.getShort(9));
        }
        return ret;
    }

    public static enum PacketType {
        UNKNOWN((byte)0, "未知类型"),
        REBACK((byte)1, "主动上报"),
        BEREBACKED((byte)2, "被动回传");

        private byte value;
        private String name;

        public byte getValue() {
            return this.value;
        }

        public String getName() {
            return this.name;
        }

        private PacketType(byte value, String name) {
            this.value = value;
            this.name = name;
        }

        public static PacketType getPacketTypeByValue(byte value) {
            PacketType[] var1 = values();
            int var2 = var1.length;

            for(int var3 = 0; var3 < var2; ++var3) {
                PacketType item = var1[var3];
                if(item.value == value) {
                    return item;
                }
            }

            return UNKNOWN;
        }
    }
}
